package com.chunuo.eduservice.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.chunuo.commonutils.R;
import com.chunuo.eduservice.entity.vo.CoursePublishVo;
import com.chunuo.eduservice.entity.vo.CourseQuery;
import com.chunuo.eduservice.entity.vo.EduCourseVo;
import com.chunuo.eduservice.service.EduCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-07-04
 */
@RestController
@RequestMapping("/eduservice/edu-course")
//@CrossOrigin
public class EduCourseController {

    @Autowired
    private EduCourseService eduCourseService;

    /**
     * 分页 条件查询
     * @param page
     * @param limit
     * @param query
     * @return
     */
    @PostMapping("/findAll/{page}/{limit}")
    public R findAll(@PathVariable("page")Integer page,
                     @PathVariable("limit")Integer limit,
                     @RequestBody CourseQuery query){
        IPage iPage = eduCourseService.findAll(page,limit,query);
        return R.ok().
            data("data",iPage.getRecords()).
            data("page",iPage.getCurrent()).
            data("total",iPage.getTotal());
    }

    @DeleteMapping("/{id}")
    public R delete(@PathVariable("id")String id){
        eduCourseService.delete(id);
        return R.ok();
    }


    @GetMapping("/getPublihCourseInfo/{id}")
    public R getPublihCourseInfo(@PathVariable("id")String id){
        CoursePublishVo publihCourseInfo = eduCourseService.getPublihCourseInfo(id);
        return R.ok().data("PublihCourseInfo",publihCourseInfo);
    }

    @PostMapping("/save")
    public R save(@RequestBody EduCourseVo eduCourseVo){
        String id = eduCourseService.saves(eduCourseVo);
        return R.ok().data("id",id);
    }

    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") String id){
        EduCourseVo eduCourseVo = eduCourseService.info(id);
        return R.ok().data("info",eduCourseVo);
    }

    @PutMapping("/edit")
    public R update(@RequestBody EduCourseVo eduCourseVo){
        String id = eduCourseService.updates(eduCourseVo);
        return R.ok().data("id",id);
    }

}

